<template>
  <div class="container user-list" v-loading="loading">
    <div class="handle-box">
      <el-input v-model="query.username" placeholder="账号" class="handle-input mr10" maxlength="20"></el-input>
      <el-button type="primary" :icon="Search" @click="handleSearch">搜索</el-button>

      <el-button type="success" :icon="Plus" @click="addUser">新增用户</el-button>
      <!-- <el-button type="primary" @click="countConsume">消耗统计</el-button> -->
    </div>

    <el-row>
      <el-table :data="users.items" border class="table" :row-key="row => row.id"
                @selection-change="handleSelectionChange" table-layout="auto">
        <el-table-column type="selection" width="38"/>
        <el-table-column prop="mobile" label="账号">
          <template #default="scope">
            <span>{{ scope.row.username }}</span>
            <el-image v-if="scope.row.vip" :src="vipImg" style="height: 20px;position: relative; top:5px; left: 5px"/>
          </template>
        </el-table-column>
        <el-table-column prop="calls" label="剩余对话次数"/>
        <!-- <el-table-column label="剩余对话次数">
          <template #default="scope">
            <span v-if="scope.row.restrict == true">{{ scope.row['calls'] }}</span>
            <el-tag v-else>不限次</el-tag>
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="img_calls" label="剩余绘图次数"/> -->
        <el-table-column prop="total_tokens" label="累计消耗tokens"/>
        <el-table-column label="状态" width="80">
          <template #default="scope">
            <el-tag v-if="scope.row.status" type="success">正常</el-tag>
            <el-tag type="danger" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="过期时间">
          <template #default="scope">
            <span v-if="scope.row['expired_time']">{{ scope.row['expired_time'] }}</span>
            <el-tag v-else>长期有效</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="注册时间">
          <template #default="scope">
            <span>{{ dateFormat(scope.row['created_at']) }}</span>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button-group class="ml-4">
              <el-button size="small" type="primary" @click="userEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="removeUser(scope.row)">删除</el-button>
              <el-button size="small" type="success" @click="resetPass(scope.row)">重置密码</el-button>
              <el-button size="small" type="warning" @click="distribute(scope.row)" v-show="scope.row.restrict == true">分配</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination v-if="users.total > 0"
                       background
                       layout="total, prev, pager, next"
                       v-model:current-page="users.page"
                       v-model:page-size="users.page_size"
                       :total="users.total"
                       @current-change="fetchUserList(users.page, users.page_size)"
        />

      </div>
    </el-row>

    <el-dialog
        v-model="showUserEditDialog"
        :title="title"
        :close-on-click-modal="false"
        width="50%"
    >
      <el-form :model="user" label-width="100px" ref="userEditFormRef" :rules="rules">
        <el-form-item label="账号：" prop="username">
          <el-input v-model="user.username" autocomplete="off" maxlength="30"/>
        </el-form-item>
        <el-form-item v-if="add" label="密码：" prop="password">
          <el-input v-model="user.password" autocomplete="off" maxlength="16"/>
        </el-form-item>
        <el-form-item label="对话次数：" prop="calls">
          <el-input v-model.number="user.calls" autocomplete="off" placeholder="0"/>
        </el-form-item>
        <!-- <el-form-item label="绘图次数：" prop="img_calls">
          <el-input v-model.number="user['img_calls']" autocomplete="off" placeholder="0"/>
        </el-form-item> -->

        <el-form-item label="有效期：" prop="expired_time">
          <el-date-picker
              v-model="user.expired_time"
              type="datetime"
              placeholder="选择日期"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              :disabled-date="disabledDate"
          />
        </el-form-item>

        <!-- <el-form-item label="聊天角色" prop="chat_roles">
          <el-select
              v-model="user.chat_roles"
              multiple
              :filterable="true"
              placeholder="选择聊天角色，多选"
          >
            <el-option
                v-for="item in roles"
                :key="item.key"
                :label="item.name"
                :value="item.key"
            />
          </el-select>
        </el-form-item> -->

        <!-- <el-form-item label="模型权限" prop="chat_models">
          <el-select
              v-model="user.chat_models"
              multiple
              :filterable="true"
              placeholder="选择AI模型，多选"
          >
            <el-option
                v-for="item in models"
                :key="item.id"
                :label="item.name"
                :value="item.value"
            />
          </el-select>
        </el-form-item> -->

        <!-- <el-form-item label="限次">
          <el-switch v-model="user.restrict"  />
        </el-form-item> -->
        <el-form-item label="启用状态">
          <el-switch v-model="user.status"/>
        </el-form-item>
       

        <!-- <el-form-item label="开通VIP">
          <el-switch v-model="user.vip"/>
        </el-form-item> -->
      </el-form>

      <template #footer>
            <span class="dialog-footer">
              <el-button @click="showUserEditDialog = false">取消</el-button>
              <el-button type="primary" @click="saveUser">提交</el-button>
            </span>
      </template>
    </el-dialog>

    <el-dialog
        v-model="showResetPassDialog"
        title="重置密码"
        width="50%"
    >
      <el-form label-width="100px" ref="userEditFormRef">
        <el-form-item label="账户：">
          <el-input v-model="pass.username" autocomplete="off" readonly disabled/>
        </el-form-item>

        <el-form-item label="新密码：">
          <el-input v-model="pass.password" autocomplete="off"/>
        </el-form-item>
      </el-form>

      <template #footer>
            <span class="dialog-footer">
              <el-button type="primary" @click="doResetPass">提交</el-button>
            </span>
      </template>

    </el-dialog>

    <el-dialog
        v-model="showDistributeDialog"
        title="分配"
        width="50%"
    >
      <el-form label-width="100px" ref="userEditFormRef">
        <el-form-item label="账户：">
          <el-input v-model="distributeData.username" autocomplete="off" readonly disabled/>
        </el-form-item>

        <el-form-item label="剩余次数：">
          <el-input v-model="distributeData.calls" autocomplete="off" readonly disabled/>
        </el-form-item>
        <el-form-item >
          <el-radio-group v-model="distributeData.distributeMethod" class="ml-4">
            <el-radio-button :label="1" >
              <el-icon><Plus /></el-icon>
            </el-radio-button>
            <el-radio-button :label="0" >
              <el-icon><Minus /></el-icon>
            </el-radio-button>
          </el-radio-group>

          <el-input style="width: 50%;margin-left: 10px;" v-model.number="distributeData.amount" autocomplete="off" placeholder="请输入分配次数"/> 
          <div class="distribute-test">可分配次数：{{ allocCalls }}</div>
        </el-form-item>
      </el-form>

      <template #footer>
            <span class="dialog-footer">
              <el-button @click="closeDistributeDialog">取消</el-button>
              <el-button type="primary" @click="doDistribute">提交</el-button>
            </span>
      </template>
      </el-dialog>  
      <el-dialog
        v-model="showCountConsumeDialog"
        title="消耗统计"
        width="55%"
    >
    <div class="count-consum-time"> 
      <div class="count-consum-time-picker">查询时间：</div>
    <el-date-picker
        
        v-model="timeRange "
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        format="YYYY-MM-DD HH:mm:ss"
        date-format="YYYY/MM/DD ddd"
        time-format="A hh:mm:ss"
      /></div>
    
      <!-- <echarts-box :EchartsData="EchartsData"></echarts-box> -->
      

    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref,watch} from "vue";
import {httpGet, httpPost} from "@/utils/http";
import {ElMessage, ElMessageBox} from "element-plus";
import {dateFormat, disabledDate, removeArrayItem} from "@/utils/libs";
import {Plus, Search,Minus} from "@element-plus/icons-vue";
import echartsBox from "../../components/admin/UserEcharts.vue"; // 引入 Echarts 组件


// 变量定义
const users = ref({page: 1, page_size: 15, items: []})
const query = ref({username: '', page: 1, page_size: 15})

const title = ref('添加用户')
const vipImg = ref("/images/vip.png")
const add = ref(true)
const user = ref({status: false, restrict: 0})
const pass = ref({username: '', password: '', id: 0})
const distributeData = ref({username: '', calls: '', id: 0,distributeMethod:"1",amount:""})
const roles = ref([])
const models = ref([])
const showUserEditDialog = ref(false)
const showResetPassDialog = ref(false)
const allocCalls = ref('')
const showDistributeDialog = ref(false) 
const showCountConsumeDialog = ref(false) 
const timeRange  = ref('') 
const EchartsData = ref({})
const rules = reactive({
  username: [
    {required: true, message: '请输入账号', trigger: 'blur'},
    {pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$|^1[3-9]\d{9}$/, message: '请输入有效的邮箱或手机号', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {pattern:/^[a-zA-Z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~\s]{8,16}$/, message: '密码长度为8-16位', trigger: 'blur'}
  ],
  calls: [
    {required: true, message: '请输入提问次数'},
    {type: 'number', message: '请输入有效数字'},
  ],
  // chat_roles: [{required: true, message: '请选择聊天角色', trigger: 'change'}],
  // chat_models: [{required: true, message: '请选择AI模型', trigger: 'change'}],
})
const loading = ref(true)

const userEditFormRef = ref(null)

onMounted(() => {
  fetchUserList(users.value.page, users.value.page_size)
  // 获取角色列表
  httpGet('/api/admin/role/list').then((res) => {
    roles.value = res.data;
  }).catch(() => {
    ElMessage.error("获取聊天角色失败");
  })
  getAllocCalls()
  // //消耗统计接口
  // httpPost('/api/admin/user/callsCount',{start_time:'2023-01-01 00:00:00',end_time:'2026-01-01 00:00:00'}).then((res) => {
  //   EchartsData.value = res.data;
  // }).catch(() => {
  //   ElMessage.error("获取消耗统计数据失败");
  // })

  httpGet('/api/admin/model/list').then(res => {
    models.value = res.data
  }).catch(e => {
    ElMessage.error("获取模型失败：" + e.message)
  })
})

const fetchUserList = function (page, pageSize) {
  query.value.page = page
  query.value.page_size = pageSize
  httpGet('/api/admin/user/list', query.value).then((res) => {
    if (res.data) {
      // 初始化数据
      const arr = res.data.items;
      for (let i = 0; i < arr.length; i++) {
        arr[i].expired_time = dateFormat(arr[i].expired_time)
      }
      users.value.items = arr
      users.value.total = res.data.total
      users.value.page = res.data.page
      user.value.page_size = res.data.page_size
    }
    loading.value = false
  }).catch(() => {
    ElMessage.error('加载用户列表失败')
  })
}

const handleSearch = () => {
  fetchUserList(users.value.page, users.value.page_size)
}

// 删除用户
const removeUser = function (user) {
  ElMessageBox.confirm(
      '此操作将会永久删除用户信息和聊天记录，确认操作吗?',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    httpGet('/api/admin/user/remove', {id: user.id}).then(() => {
      ElMessage.success('操作成功！')
      users.value.items = removeArrayItem(users.value.items, user, function (v1, v2) {
        return v1.id === v2.id
      })
    }).catch((e) => {
      ElMessage.error('操作失败，' + e.message)
    })
  }).catch(() => {
    ElMessage.info('操作被取消')
  })

}

const userEdit = function (row) {
  user.value = row
  title.value = '编辑用户'
  showUserEditDialog.value = true
  add.value = false
}

const addUser = () => {
  user.value = {}
  title.value = '添加用户'
  showUserEditDialog.value = true
  add.value = true
}

const saveUser = function () {
  userEditFormRef.value.validate((valid) => {
    if (valid) {
      showUserEditDialog.value = false
      httpPost('/api/admin/user/save', user.value).then((res) => {
        ElMessage.success('操作成功！')
        if (add.value) {
          users.value.items.push(res.data)
        }
      }).catch((e) => {
        ElMessage.error('操作失败，' + e.message)
      })
    } else {
      return false
    }
  })
}

const handleSelectionChange = function (rows) {
  // TODO: 批量删除操作
  console.log(rows)
}

const resetPass = (row) => {
  showResetPassDialog.value = true
  pass.value.id = row.id
  pass.value.username = row.username
}

const doResetPass = () => {
  httpPost('/api/admin/user/resetPass', pass.value).then(() => {
    ElMessage.success('操作成功！')
    showResetPassDialog.value = false
  }).catch((e) => {
    ElMessage.error('操作失败，' + e.message)
  })
}
const getAllocCalls = () => {
  httpGet('/api/admin/dashboard/stats').then((res) => {
    allocCalls.value = res.data.allocCalls
  }).catch((e) => {
    ElMessage.error('获取失败，' + e.message)
  })
}

const distribute = (row) => {
  showDistributeDialog.value = true
  distributeData.value.id = row.id
  distributeData.value.username = row.username
  distributeData.value.calls = row.calls
}

const doDistribute = () => {
  if(distributeData.value.distributeMethod === 0){
    distributeData.value.amount = -Math.abs(distributeData.value.amount)
  }
  
  httpPost('/api/admin/user/divideCallsToUser', {userid:distributeData.value.id,num:distributeData.value.amount}).then(() => {
    ElMessage.success('操作成功！')
    closeDistributeDialog()
    fetchUserList(users.value.page, users.value.page_size)
    getAllocCalls()
  }).catch((e) => {
    ElMessage.error('操作失败，' + e.message)
  })
}
const closeDistributeDialog = () => {
  showDistributeDialog.value = false
  distributeData.value.amount = ""
}
const countConsume = () => {
  showCountConsumeDialog.value = true
}
//日期格式转化函数
const formatTime = (timeString) => {
  const date = new Date(timeString);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
// 监听timeRange变化
watch(
      () => timeRange.value,
      async (newTimeRange, oldTimeRange) => {
        if (newTimeRange && Array.isArray(newTimeRange) && newTimeRange.length === 2) {
     

      const startTime = formatTime(newTimeRange[0]);
      const endTime = formatTime(newTimeRange[1]);

          try {
            const res = await httpPost('/api/admin/user/callsCount', {
              start_time: startTime,
              end_time: endTime,
            });
            EchartsData.value = res.data;
          } catch (error) {
            ElMessage.error("请求失败");
          }
        }
      },
      { immediate: true } 
    );

</script>

<style lang="stylus" scoped>
.user-list {

  .handle-box {
    .handle-input {
      max-width 150px;
      margin-right 10px;
    }
  }

  .table {
    width 100%
  }

  .opt-box {
    padding-bottom: 10px;

    .el-icon {
      margin-right: 5px;
    }
  }

  .el-select {
    width: 100%
  }
  .distribute-test {
    margin-left:8px;
  }
  
  .count-consum-time{
    margin-left:8px;
    margin-bottom: 20px;
  }
  .count-consum-time-picker{
    margin-right:20px;
    display: inline-block
  }

}
</style>